#include "MainLayer.h"
#include "MainPalette/MainPalette.h"

USING_NS_CC;

static MainLayer* g_MainLayer;

CCScene* MainLayer::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::node();

	// 'layer' is an autorelease object
	MainLayer *layer = MainLayer::node();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool MainLayer::init()
{
	g_MainLayer = this;
	m_nFingerStatus = 0;
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	/////////////////////////////
	// 2. add a menu item with "X" image, which is clicked to quit the program
	//    you may modify it.

	// add a "close" icon to exit the progress. it's an autorelease object
 	CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
 										"Images\\UI\\CloseNormal.png",
 										"Images\\UI\\CloseSelected.png",
 										this,
 										menu_selector(MainLayer::menuCloseCallback) );
 	pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
 
 	// create menu, it's an autorelease object
 	CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
 	pMenu->setPosition( CCPointZero );
 	this->addChild(pMenu, 1);


	pCloseItem = CCMenuItemImage::itemFromNormalImage(
		"Images\\UI\\CloseNormal.png",
		"Images\\UI\\CloseSelected.png",
		this,
		menu_selector(MainLayer::menuRestartCallback) );
	pCloseItem->setPosition( ccp(20, 20) );

	// create menu, it's an autorelease object
	pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
	pMenu->setPosition( CCPointZero );
	this->addChild(pMenu, 1);


	pCloseItem = CCMenuItemImage::itemFromNormalImage(
		"Images\\UI\\CloseNormal.png",
		"Images\\UI\\CloseSelected.png",
		this,
		menu_selector(MainLayer::menuSwitchCallback) );
	pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width * 0.5f, 20) );

	// create menu, it's an autorelease object
	pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
	pMenu->setPosition( CCPointZero );
	this->addChild(pMenu, 1);



	/////////////////////////////
	// 3. add your codes below...

	// add a label shows "Hello World"
	// create and initialize a label
    m_pTouchPos = CCLabelTTF::labelWithString("Color Palette", "Arial", 12);
	// ask director the window size
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	// position the label on the center of the screen
	m_pTouchPos->setPosition( ccp(size.width / 2, size.height - 10) );
	// add the label as a child to this layer
	this->addChild(m_pTouchPos, 1);




	//////////////////////////////////////////////////////////////////////////
	// 4. create palette
	m_pPalette5x5 = new CMainPalette(this, SPaletteFrame(5, 59.2f, 14.f, 35.f, "Images\\Palette\\plt5x5.png"));
	if (m_pPalette5x5)
	{
		m_pPalette5x5->init();
		m_pPalette5x5->hide();
	}

	m_pPalette4x4 = new CMainPalette(this, SPaletteFrame(4, 74.f, 14.f, 35.f, "Images\\Palette\\plt4x4.png"));
	if (m_pPalette4x4)
	{
		m_pPalette4x4->init();
		m_pPalette4x4->hide();
	}

	m_pPalette3x3 = new CMainPalette(this, SPaletteFrame(3, 98.2f, 14.f, 35.f, "Images\\Palette\\plt3x3.png"));
	if (m_pPalette3x3)
	{
		m_pPalette3x3->init();
		m_pPalette3x3->hide();
	}

	m_pPalette2x2 = new CMainPalette(this, SPaletteFrame(2, 148.f, 14.f, 35.f, "Images\\Palette\\plt2x2.png"));
	if (m_pPalette2x2)
	{
		m_pPalette2x2->init();
		m_pPalette2x2->hide();
	}

	m_pPaletteCurr = m_pPalette2x2;
	m_pPaletteCurr->display();
	
	//////////////////////////////////////////////////////////////////////////
	// 5. register Scheduller

	this->scheduleUpdate();
	this->registerWithTouchDispatcher();


	return true;
}

void MainLayer::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();

	if (m_pPalette5x5)
	{
		m_pPalette5x5->destroy();
		delete m_pPalette5x5;
	}

	if (m_pPalette4x4)
	{
		m_pPalette4x4->destroy();
		delete m_pPalette4x4;
	}

	if (m_pPalette3x3)
	{
		m_pPalette3x3->destroy();
		delete m_pPalette3x3;
	}

	if (m_pPalette2x2)
	{
		m_pPalette2x2->destroy();
		delete m_pPalette2x2;
	}
	

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}

//////////////////////////////////////////////////////////////////////////
void MainLayer::menuRestartCallback( CCObject* pSender )
{
	m_pPaletteCurr->restart();
}

//////////////////////////////////////////////////////////////////////////
void MainLayer::menuSwitchCallback( CCObject* pSender )
{
	static int currPalette = 0;
	currPalette++;

	if (currPalette == 4)
	{
		currPalette = 0;
	}

	switch (currPalette)
	{
		case 0:
			m_pPaletteCurr = m_pPalette2x2;
			break;
		case 1:
			m_pPaletteCurr = m_pPalette3x3;
			break;
		case 2:
			m_pPaletteCurr = m_pPalette4x4;
			break;
		case 3:
			m_pPaletteCurr = m_pPalette5x5;
			break;
	}

	m_pPalette2x2->hide();
	m_pPalette3x3->hide();
	m_pPalette4x4->hide();
	m_pPalette5x5->hide();

	m_pPaletteCurr->display();
	m_pPaletteCurr->restart();
}


//////////////////////////////////////////////////////////////////////////
void MainLayer::draw( void )
{
	if (m_nFingerStatus)
	{
		glColor4ub(255, 200, 200, 255);
		glLineWidth(2.0f);
		ccDrawLine( m_fingerStartGL, m_fingerStopGL );
		char widget[255];
		sprintf_s( widget, "sx=%.2f sy=%.2f ex=%.2f ey=%.2f", m_fingerStartGL.x, m_fingerStartGL.y, m_fingerStopGL.x, m_fingerStopGL.y );
		m_pTouchPos->setString(widget);
		m_pTouchPos->setIsVisible(true);
	}
	else
	{
		m_pTouchPos->setIsVisible(false);
	}
}


void MainLayer::update( ccTime dt )
{

}

cocos2d::CCLayer* MainLayer::getRootLayer()
{
	return g_MainLayer;
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchesBegan( CCSet *pTouches, CCEvent *pEvent )
{
	//throw std::exception("The method or operation is not implemented.");
	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);

	m_nFingerStatus = 1;
	m_fingerStart = touch->locationInView( touch->view() );	
	m_fingerStartGL = CCDirector::sharedDirector()->convertToGL( m_fingerStart );
	m_fingerStopGL = m_fingerStartGL;
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchesMoved( CCSet *pTouches, CCEvent *pEvent )
{
	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);

	m_fingerStop = touch->locationInView( touch->view() );	
	m_fingerStopGL = CCDirector::sharedDirector()->convertToGL( m_fingerStop );
	//throw std::exception("The method or operation is not implemented.");
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchesEnded( CCSet *pTouches, CCEvent *pEvent )
{
	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);

	m_nFingerStatus = 0;
	m_fingerStop = touch->locationInView( touch->view() );	
	m_fingerStopGL = CCDirector::sharedDirector()->convertToGL( m_fingerStop );


	// now, execute operation
	m_pPaletteCurr->execute(m_fingerStartGL, m_fingerStopGL);


	//throw std::exception("The method or operation is not implemented.");
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchesCancelled( CCSet *pTouches, CCEvent *pEvent )
{
	//throw std::exception("The method or operation is not implemented.");
}
//////////////////////////////////////////////////////////////////////////
bool MainLayer::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent )
{
	//throw std::exception("The method or operation is not implemented.");
//	m_nFingerStatus = 1;
//	m_fingerStart = pTouch->locationInView(pTouch->view());

	return true;
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchMoved( CCTouch *pTouch, CCEvent *pEvent )
{
	//throw std::exception("The method or operation is not implemented.");
//	m_fingerStop = pTouch->locationInView(pTouch->view());
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent )
{
	//throw std::exception("The method or operation is not implemented.");
//	m_nFingerStatus = 0;
//	m_fingerStop = pTouch->locationInView(pTouch->view());
}
//////////////////////////////////////////////////////////////////////////
void MainLayer::ccTouchCancelled( CCTouch *pTouch, CCEvent *pEvent )
{
	//throw std::exception("The method or operation is not implemented.");
}

